<?php
 class m_pemberian_beasiswa extends Model{
     var $table = 'keuangan_adik_asuh',
     $table2 = 'pemberian_beasiswa';
     function m_pemberian_beasiswa(){
         parent::Model();
     }
     function addBeasiswa($data){
         $return = array();
         
         $qcek = $this->db->query("select count(*) as cek from keuangan_adik_asuh where id_adik_asuh = '$data[id_adik_asuh]' and id_tahun_ajaran = '$data[id_tahun_ajaran]' and saldo = 180000");
         foreach ($qcek->result_array() as $cek);
         if($cek['cek'] > 0){
             $return['status'] = FALSE;
         }else{
             $dataInsert = array('tanggal' => $data['tanggal'],
                 'id_petugas' => $this->session->userdata('id_member'),
                 'id_adik_asuh' => $data['id_adik_asuh'],
                 'id_sekolah' => $data['id_sekolah'],
                 'id_kelas' => $data['id_kelas'],
                 'id_tahun_ajaran' => $data['id_tahun_ajaran'],
                 'debet' => $data['jumlah'],
                 'saldo' => (($data['total_debet']-$data['total_kredit'])+$data['jumlah']),
                 'keterangan' => $data['keterangan']);
             $dataInsert2 = array('tanggal' => $data['tanggal'],
                 'id_petugas' => $this->session->userdata('id_member'),
                 'id_adik_asuh' => $data['id_adik_asuh'],
                 'id_sekolah' => $data['id_sekolah'],
                 'id_kelas' => $data['id_kelas'],
                 'id_tahun_ajaran' => $data['id_tahun_ajaran'],
                 'jumlah' => $data['jumlah'],
                 'keterangan' => $data['keterangan'],
                 'status' => '0');
             $insert = $this->db->insert($this->table,$dataInsert);
             $id = $this->db->insert_id();
             $insert2 = $this->db->insert($this->table2,$dataInsert2);
             $return['status'] = TRUE;
             $return['id'] = $id;
         }
         
        return $return;
     }
     function getPemberianBeasiswa($bulan = NULL,$tahun = NULL){
         $tanggal = betweenDate($bulan, $tahun);
         $query = $this->db->query("select pb.id as id_pemberian,m.nama as petugas,pb.tanggal,pb.jumlah,pb.keterangan,pb.status,a.nama as adik_asuh,s.nama as sekolah,k.nama as kelas,tp.nama as tingkat_pendidikan,ta.nama as tahun_ajaran,sms.nama as semester from pemberian_beasiswa pb 
         join member m on pb.id_petugas = m.id
         join adik_asuh a on pb.id_adik_asuh = a.id
         join sekolah s on pb.id_sekolah = s.id
         join kelas k on pb.id_kelas = k.id
         join tingkat_pendidikan tp on k.id_tingkat_pendidikan = tp.id
         join tahun_ajaran ta on pb.id_tahun_ajaran = ta.id
         join semester sms on ta.id_semester = sms.id 
         where pb.tanggal between '$tanggal[awal]' and '$tanggal[akhir]'");
         
         if($query->num_rows() > 0){
            return $query->result_array();
         }else return array();
     }
     function unapprovePemberianBeasiswa($id,$bulan,$tahun,$jumlah){
        $updateRekap = $this->db->query("update rekap_keuangan_bulanan set jumlah = (jumlah+$jumlah) where id = '".$bulan.$tahun."'"); 
        $query = $this->db->query("update pemberian_beasiswa set status = '0' where id = '$id'");
        if($query) return TRUE;
     }
 }
?>
